using System;
using KingPang.hohoBlog.API;
using System.Net;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Security.Permissions;
using System.Web;

namespace KingPang.hohoBlog
{
    /// <summary>
    /// This demo application demonstrate blogging to LiveJournal programmatically
    /// </summary>
    class BlogLiveJournal
    {
        /// <summary>
        /// The following code executes when program starts
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            /* Put your configuration below */
            string username = "YOUR LIVEJOURNAL USERNAME";
            string password = "YOUR LIVEJOURNAL PASSWORD";
            string subject = "This is subject";
            string body = "This is a message body";
            /* End of configuration */


            LiveJournal lj = new LiveJournal();
            if (lj.Login(username, password))
            {
                lj.AddBlog(subject, body);
                System.Console.WriteLine("Blog successfully!");
            }
            else
            {
                System.Console.WriteLine("Blog failed");
            }
            System.Console.WriteLine("Press any key to close the application...");
            System.Console.ReadLine();

        }

        private static void testLogin()
        {
            string strLoginName = "dennis_zsc";
            string strPassword = "dennis&quiet";

            try
            {
                string loginUir = "http://reg.hexun.com/login.aspx";
                //loginUir += "?gourl=http://t.hexun.com/home.html";
                //string loginReferUri = "http://t.hexun.com/rest/indexlogin.aspx";

                string strURL = loginUir;
                //string strPostData = String.Format("login={0}&passwd={1}", strLoginName.Trim(), strPassword.Trim());
                string strPostData = string.Format("TextBoxUserName={0}&TextBoxPassword={1}", strLoginName, strPassword);

                // Setup the http request.
                HttpWebRequest webReq = WebRequest.Create(strURL) as HttpWebRequest;
                webReq.Method = "POST";
                webReq.ContentLength = strPostData.Length;
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.AllowAutoRedirect = true;
                webReq.CookieContainer = new CookieContainer();

                // Post to the login form.
                StreamWriter streamRequestWriter = new StreamWriter(webReq.GetRequestStream());
                streamRequestWriter.Write(strPostData);
                streamRequestWriter.Close();

                // Get the response.
                HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();

                // Have some cookies.
                CookieCollection cookieCollection = webResp.Cookies;

                // Read the response

                Stream datastream = webResp.GetResponseStream();
                StreamReader reader = new StreamReader(datastream, System.Text.Encoding.GetEncoding(webResp.CharacterSet), true);
                String strResponseFromServer = reader.ReadToEnd();

                Console.WriteLine(strResponseFromServer);
                Console.ReadLine();
            }

            catch (WebException e)
            {
                Console.WriteLine("\nMain 1 Exception raised!");
                Console.WriteLine("\nMessage 1:{0}", e.Message);
                Console.WriteLine("\nStatus 1:{0}", e.Status);
                Console.WriteLine("Press any key to continue..........");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("\nMain 2 Exception raised!");
                Console.WriteLine("Source 2:{0} ", e.Source);
                Console.WriteLine("Message 2:{0} ", e.Message);
                Console.WriteLine("Press key to continue..........");
                Console.ReadLine();
            }
        }
    }
}
